import cv2
import pickle
import numpy as np
import gradio as gr
# 加载保存的模型
with open('clf_model.pkl', 'rb') as file:
    clf = pickle.load(file)
def image1(image):
    # 读取原分割图（灰度图）
    segment = cv2.imread('Sandstone_2_segment.tif', cv2.IMREAD_GRAYSCALE)
    # 对原图进行预测
    features = image.reshape(-1, 3)
    predictions = clf.predict(features)
    segmentation = predictions.reshape(image.shape[:2])
    # 计算准确率
    accuracy = np.mean(segmentation == segment)
    # 转换为灰度图
    segment_gray = cv2.cvtColor(segment, cv2.COLOR_GRAY2BGR)
    segmentation_gray = cv2.cvtColor(segmentation, cv2.COLOR_GRAY2BGR)
    return segment_gray, segmentation_gray
# 创建Gradio界面
iface = gr.Interface(fn=image1, inputs="image", outputs=["image", "image"])
iface.launch()